package workqueues.acknowledgment;

import com.rabbitmq.client.CancelCallback;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.DeliverCallback;
import utils.RabbitMQUtil;
import utils.SleepUtil;

import java.nio.charset.StandardCharsets;

public class Worker02 {

    public static final String TASK_QUEUE_NAME = "ack_queue";

    public static void main(String[] args) throws Exception{

        Channel channel = RabbitMQUtil.getChannel();
        System.out.println("C1等待接收消息处理时间较长");

        DeliverCallback deliverCallback = (consumerTag, message) ->{
            // 睡眠十秒钟
            SleepUtil.sleep(10);
            System.out.println("接收到的消息："+new String(message.getBody(), StandardCharsets.UTF_8));

            int prefetchCount = 5;
            channel.basicQos(prefetchCount);
            channel.basicAck(message.getEnvelope().getDeliveryTag(),false);
        };

        // 消息接收被取消时，执行下面的内容
        CancelCallback cancelCallback = consumerTag -> {
            System.out.println(consumerTag+"消费者取消消费接口回调逻辑");
        };
        channel.basicConsume(TASK_QUEUE_NAME, false,deliverCallback,cancelCallback);
    }
}
